package info.ask4research.dao.impl;

import info.ask4research.dao.IUnitOfLearningDAO;
import info.ask4research.dao.core.impl.AbstractDaoImpl;
import info.ask4research.model.UnitOfLearning;
import info.ask4research.model.activities.Activity;
import info.ask4research.web.dto.UnitOfLearningDTO;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;

@Repository("unitOfLearningDAO")
public class UnitOfLearningDAO extends AbstractDaoImpl<UnitOfLearning> implements IUnitOfLearningDAO {

	public UnitOfLearningDAO() {
		super(UnitOfLearning.class);
	}
	
	private HibernateTemplate hibernateTemplate;

	@Autowired
	public void setSessionFactory(SessionFactory sessionFactory) {
		hibernateTemplate = new HibernateTemplate(sessionFactory);
	}

	@Override
	public List<UnitOfLearning> getUnitOfLearningPaginated(final Integer limit,
			Integer page, final Integer start) {
		@SuppressWarnings("unchecked")
		List<UnitOfLearning> unitOfLearnins = hibernateTemplate.executeFind(new HibernateCallback() {  
			             public Object doInHibernate(Session session) throws HibernateException, SQLException {  
				                 Query query = (Query) session.createQuery("from UnitOfLearning uol");  
				                 query.setMaxResults(limit);  
				                 query.setFirstResult(start);  
				                 return query.list();  
				             }  
				         }); 
		
		return unitOfLearnins;
	}

	@Override
	public List<UnitOfLearningDTO> getUnitOfLearningPaginated(final Integer limit,
			Integer page, final Integer start, final String queryString) {
		@SuppressWarnings("unchecked")
		List<Object[]> unitOfLearnins = hibernateTemplate.executeFind(new HibernateCallback() {  
			             public List<Object[]> doInHibernate(Session session) throws HibernateException, SQLException {  
				                 Query query = (Query) session.createQuery("select uol.id, uol.title,uol.description from UnitOfLearning uol where uol.title like :queryString");
				                 query.setParameter("queryString", "%"+queryString+"%");
				                 query.setMaxResults(limit);  
				                 query.setFirstResult(start);  
				                 return query.list();  
				             }  
				         }); 
		List<UnitOfLearningDTO> dto = new ArrayList<UnitOfLearningDTO>();
		for(int i=0;i<unitOfLearnins.size(); i++){
			UnitOfLearningDTO unitOfLearningDTO = new UnitOfLearningDTO();
			unitOfLearningDTO.setId(String.valueOf(unitOfLearnins.get(i)[0]));
			unitOfLearningDTO.setDescription((String) unitOfLearnins.get(i)[1]);
			unitOfLearningDTO.setTitle((String) unitOfLearnins.get(i)[2]);
			dto.add(unitOfLearningDTO);
		}
		return dto;
	}
	
	@Override
	public boolean existByTitle(final String title) {
		List<Object[]> unitOfLearnins = hibernateTemplate.executeFind(new HibernateCallback() {  
            public List<Object[]> doInHibernate(Session session) throws HibernateException, SQLException {  
	                 Query query = (Query) session.createQuery("select uol from UnitOfLearning uol where uol.title =:queryString");
	                 query.setParameter("queryString", title);
	                 return query.list();  
	             }  
	         }); 
		if(unitOfLearnins.isEmpty()){
			return false;
		}else{
			return true;	
		}
	}

	@Override
	public List<Activity> getActivities(final int uoLId) {

		List<Activity> acts = hibernateTemplate.executeFind(new HibernateCallback() {  
            public List<Object[]> doInHibernate(Session session) throws HibernateException, SQLException {  
	                 Query query = (Query) session.createQuery("select uol.activity from UnitOfLearning uol where uol.id =:queryString");
	                 query.setParameter("queryString", uoLId);
	                 return query.list();  
	             }  
	         }); 
		return acts;
	}



}
